Switch Statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax of Switch Statement


switch (variable) {
    case value1:
        // Statements
        break;
    case value2:
        // Statements
        break;
    ...
    default:
        // Default statements
        break;
}

            

Flowchart of Switch Statement

Example


#include < stdio.h>
void main() {
    int day;
    printf("Enter the day: ");
    scanf("%d", &day);

    switch (day) {
        case 1:
            printf("Sunday");
            break;
        case 2:
            printf("Monday");
            break;
        case 3:
            printf("Tuesday");
            break;
        case 4:
            printf("Wednesday");
            break;
        case 5:
            printf("Thursday");
            break;
        case 6:
            printf("Friday");
            break;
        case 7:
            printf("Saturday");
            break;
        default:
            printf("Invalid day");
            break;
    }
}